package com.bf.electroplating.controller;

import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.service.compoent.HJ212TestClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * HJ212测试控制器
 * 用于测试HJ212客户端功能
 */
@Slf4j
@RestController
@RequestMapping("/api/hj212/test")
public class HJ212TestController {
    
    @Autowired
    private HJ212TestClient hj212TestClient;
    
    /**
     * 连接到HJ212服务器
     */
    @PostMapping("/connect")
    public ResultVO<Boolean> connectToServer(@RequestBody Map<String, Object> request) {
        try {
            String host = (String) request.get("host");
            Integer port = (Integer) request.get("port");
            
            if (host == null || port == null) {
                return ResultVO.fail("服务器地址和端口不能为空");
            }
            
            boolean success = hj212TestClient.connectToServer(host, port);
            if (success) {
                return ResultVO.ok(true, "连接成功，开始监听服务器指令");
            } else {
                return ResultVO.fail("连接失败，请检查服务器地址和端口");
            }
        } catch (Exception e) {
            log.error("连接服务器失败", e);
            return ResultVO.fail("连接服务器失败");
        }
    }
    
    /**
     * 断开与服务器的连接
     */
    @PostMapping("/disconnect")
    public ResultVO<String> disconnectFromServer() {
        try {
            hj212TestClient.disconnectFromServer();
            return ResultVO.ok("已断开连接");
        } catch (Exception e) {
            log.error("断开连接失败", e);
            return ResultVO.fail("断开连接失败");
        }
    }
    
    /**
     * 检查连接状态
     */
    @GetMapping("/status")
    public ResultVO<Boolean> getConnectionStatus() {
        try {
            boolean connected = hj212TestClient.isConnected();
            return ResultVO.ok(connected);
        } catch (Exception e) {
            log.error("获取连接状态失败", e);
            return ResultVO.fail("获取连接状态失败");
        }
    }
    
    /**
     * 发送标准HJ212数据包
     */
    @PostMapping("/send-standard")
    public ResultVO<Boolean> sendStandardPacket() {
        try {
            if (!hj212TestClient.isConnected()) {
                return ResultVO.fail("未连接到服务器，请先连接");
            }
            
            boolean success = hj212TestClient.sendStandardPacket();
            if (success) {
                return ResultVO.ok(true, "标准数据包发送成功");
            } else {
                return ResultVO.fail("标准数据包发送失败");
            }
        } catch (Exception e) {
            log.error("发送标准数据包失败", e);
            return ResultVO.fail("发送标准数据包失败");
        }
    }
    
    /**
     * 发送实时数据包
     */
    @PostMapping("/send-realtime")
    public ResultVO<Boolean> sendRealTimeData(@RequestBody Map<String, String> request) {
        try {
            if (!hj212TestClient.isConnected()) {
                return ResultVO.fail("未连接到服务器，请先连接");
            }
            
            String pollutantCode = request.get("pollutantCode");
            String value = request.get("value");
            
            if (pollutantCode == null || value == null) {
                return ResultVO.fail("污染物代码和数值不能为空");
            }
            
            boolean success = hj212TestClient.sendRealTimeData(pollutantCode, value);
            if (success) {
                return ResultVO.ok(true, "实时数据包发送成功");
            } else {
                return ResultVO.fail("实时数据包发送失败");
            }
        } catch (Exception e) {
            log.error("发送实时数据包失败", e);
            return ResultVO.fail("发送实时数据包失败");
        }
    }
    
    /**
     * 发送自定义测试数据包
     */
    @PostMapping("/send-custom")
    public ResultVO<Boolean> sendCustomPacket(@RequestBody Map<String, String> request) {
        try {
            if (!hj212TestClient.isConnected()) {
                return ResultVO.fail("未连接到服务器，请先连接");
            }
            
            String testData = request.get("testData");
            if (testData == null || testData.trim().isEmpty()) {
                return ResultVO.fail("测试数据不能为空");
            }
            
            boolean success = hj212TestClient.sendTestPacket(testData);
            if (success) {
                return ResultVO.ok(true, "自定义数据包发送成功");
            } else {
                return ResultVO.fail("自定义数据包发送失败");
            }
        } catch (Exception e) {
            log.error("发送自定义数据包失败", e);
            return ResultVO.fail("发送自定义数据包失败");
        }
    }
    
    /**
     * 一次性发送测试数据包（不保持连接）
     */
    @PostMapping("/send-once")
    public ResultVO<String> sendOncePacket(@RequestBody Map<String, Object> request) {
        try {
            String host = (String) request.get("host");
            Integer port = (Integer) request.get("port");
            String testData = (String) request.get("testData");
            
            if (host == null || port == null || testData == null) {
                return ResultVO.fail("服务器地址、端口和测试数据不能为空");
            }
            
            String response = hj212TestClient.sendTestPacket(host, port, testData);
            if (response != null) {
                return ResultVO.ok(response, "数据包发送成功");
            } else {
                return ResultVO.fail("数据包发送失败或未收到响应");
            }
        } catch (Exception e) {
            log.error("发送一次性数据包失败", e);
            return ResultVO.fail("发送一次性数据包失败");
        }
    }
    
    /**
     * 获取客户端信息
     */
    @GetMapping("/info")
    public ResultVO<Map<String, Object>> getClientInfo() {
        try {
            Map<String, Object> info = new HashMap<>();
            info.put("connected", hj212TestClient.isConnected());
            info.put("description", "HJ212测试客户端 - 支持持续连接和指令监听");
            info.put("features", new String[]{
                "持续连接模式",
                "自动监听服务器指令",
                "支持多种指令处理",
                "实时数据发送",
                "标准HJ212协议支持"
            });
            
            return ResultVO.ok(info);
        } catch (Exception e) {
            log.error("获取客户端信息失败", e);
            return ResultVO.fail("获取客户端信息失败");
        }
    }
} 